home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 January / PC-WELT 01-2001.ISO / software / linux / netscap6 / xpi / browser.xpi / bin / chrome / toolkit.jar / content / global / printdialog.js < prev    next >
Encoding:
JavaScript  |  2000-09-13  |  8.6 KB  |  281 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape Communications
  16.  * Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s): Masaki Katakai <katakai@japan.sun.com>
  21.  */
  22.  
  23. var dialog;
  24. var prefs = null;
  25.  
  26. var default_command = "lpr";
  27. var default_file = "mozilla.ps";
  28.  
  29. function initDialog()
  30. {
  31.   dialog = new Object;
  32.  
  33.   dialog.findKey         = document.getElementById("dialog.findKey");
  34.   dialog.caseSensitive   = document.getElementById("dialog.caseSensitive");
  35.   dialog.wrap            = document.getElementById("dialog.wrap");
  36.   dialog.searchBackwards = document.getElementById("dialog.searchBackwards");
  37.   dialog.find            = document.getElementById("ok");
  38.   dialog.cancel          = document.getElementById("cancel");
  39.  
  40.   dialog.fileLabel       = document.getElementById("fileLabel");
  41.   dialog.cmdLabel        = document.getElementById("cmdLabel");
  42.  
  43.   dialog.fileRadio       = document.getElementById("fileRadio");
  44.   dialog.printerRadio    = document.getElementById("printerRadio");
  45.  
  46.   dialog.firstRadio      = document.getElementById("firstRadio");
  47.   dialog.lastRadio       = document.getElementById("lastRadio");
  48.  
  49.   dialog.colorRadio      = document.getElementById("colorRadio");
  50.   dialog.grayRadio       = document.getElementById("grayRadio");
  51.  
  52.   dialog.a4Radio         = document.getElementById("a4Radio");
  53.   dialog.letterRadio     = document.getElementById("letterRadio");
  54.   dialog.legalRadio      = document.getElementById("legalRadio");
  55.   dialog.exectiveRadio   = document.getElementById("exectiveRadio");
  56.  
  57.   dialog.topInput        = document.getElementById("topInput");
  58.   dialog.bottomInput     = document.getElementById("bottomInput");
  59.   dialog.leftInput       = document.getElementById("leftInput");
  60.   dialog.rightInput      = document.getElementById("rightInput");
  61.  
  62.   dialog.cmdInput        = document.getElementById("cmdInput");
  63.   dialog.fileInput       = document.getElementById("fileInput");
  64.   dialog.chooseButton    = document.getElementById("chooseFile");
  65.  
  66.   dialog.print           = document.getElementById("ok");
  67.  
  68.   dialog.enabled         = false;
  69. }
  70.  
  71. function checkValid(elementID)
  72. {
  73.   var editField = document.getElementById( elementID );
  74.   if ( !editField )
  75.     return;
  76.   var stringIn = editField.value;
  77.   if (stringIn && stringIn.length > 0)
  78.   {
  79.     stringIn = stringIn.replace(/[^\.|^0-9]/g,"");
  80.     if (!stringIn) stringIn = "";
  81.     editField.value = stringIn;
  82.   }
  83. }
  84.  
  85. function doPrintToFile( value )
  86. {
  87.   if (value == true ) {
  88.     dialog.fileLabel.removeAttribute("disabled");
  89.     dialog.cmdLabel.setAttribute("disabled","true" );
  90.     dialog.fileInput.removeAttribute("disabled");
  91.     dialog.chooseButton.removeAttribute("disabled");
  92.     dialog.cmdInput.setAttribute("disabled","true" );
  93.   } else {
  94.     dialog.cmdLabel.removeAttribute("disabled");
  95.     dialog.fileLabel.setAttribute("disabled","true" );
  96.     dialog.fileInput.setAttribute("disabled","true" );
  97.     dialog.chooseButton.setAttribute("disabled","true" );
  98.     dialog.cmdInput.removeAttribute("disabled");
  99.   }
  100. }
  101.  
  102. function loadDialog()
  103. {
  104.   var print_tofile = false;
  105.   var print_reversed = false;
  106.   var print_color = true;
  107.   var print_landscape = true;
  108.   var print_paper_size = 0;
  109.   var print_margin_top = 500;
  110.   var print_margin_left = 500;
  111.   var print_margin_bottom = 0;
  112.   var print_margin_right = 0;
  113.   var print_command = default_command;
  114.   var print_file = default_file;
  115.  
  116.   try {
  117.     prefs = Components.classes["@mozilla.org/preferences;1"];
  118.     if (prefs) {
  119.       prefs = prefs.getService();
  120.       if (prefs)
  121.         prefs = prefs.QueryInterface(Components.interfaces.nsIPref);
  122.     }
  123.   } catch(e) { }
  124.  
  125.   if (prefs) {
  126.     try { print_tofile = prefs.GetBoolPref("print.print_tofile"); } catch(e) { }
  127.     try { print_reversed = prefs.GetBoolPref("print.print_reversed"); } catch(e) { }
  128.     try { print_color = prefs.GetBoolPref("print.print_color"); } catch(e) { }
  129.     try { print_paper_size = prefs.GetIntPref("print.print_paper_size"); } catch(e) { }
  130.     try { print_margin_top = prefs.GetIntPref("print.print_margin_top"); } catch(e) { }
  131.     try { print_margin_left = prefs.GetIntPref("print.print_margin_left"); } catch(e) { }
  132.     try { print_margin_bottom = prefs.GetIntPref("print.print_margin_bottom"); } catch(e) { }
  133.     try { print_margin_right = prefs.GetIntPref("print.print_margin_right"); } catch(e) { }
  134.     try { print_command = prefs.CopyCharPref("print.print_command"); } catch(e) { }
  135.     try { print_file = prefs.CopyCharPref("print.print_file"); } catch(e) { }
  136.   }
  137.  
  138.   if ( print_tofile == true) {
  139.     dialog.fileRadio.checked = true;
  140.     doPrintToFile( true );
  141.   } else {
  142.     dialog.printerRadio.checked = true;
  143.     doPrintToFile( false );
  144.   }
  145.  
  146.   if ( print_color == true) {
  147.     dialog.colorRadio.checked = true;
  148.   } else {
  149.     dialog.grayRadio.checked = true;
  150.   }
  151.  
  152.   if ( print_reversed == true) {
  153.     dialog.lastRadio.checked = true;
  154.   } else {
  155.     dialog.firstRadio.checked = true;
  156.   }
  157.  
  158.   if ( print_paper_size == 0 ) {
  159.     dialog.letterRadio.checked = true;
  160.   } else if ( print_paper_size == 1 ) {
  161.     dialog.legalRadio.checked = true;
  162.   } else if ( print_paper_size == 2 ) {
  163.     dialog.exectiveRadio.checked = true;
  164.   } else if ( print_paper_size == 3 ) {
  165.     dialog.a4Radio.checked = true;
  166.   }
  167.  
  168.   dialog.topInput.value = print_margin_top * 0.001;
  169.   dialog.bottomInput.value = print_margin_bottom * 0.001;
  170.   dialog.leftInput.value = print_margin_left * 0.001;
  171.   dialog.rightInput.value = print_margin_right * 0.001;
  172.  
  173.   dialog.cmdInput.value = print_command;
  174.   dialog.fileInput.value = print_file;
  175.  
  176.   dialog.print.setAttribute("value",
  177.              document.getElementById("printButton").getAttribute("value"));
  178. }
  179.  
  180. var param;
  181.  
  182. function onLoad()
  183. {
  184.   // Init dialog.
  185.   initDialog();
  186.  
  187.   // setup the dialogOverlay.xul button handlers
  188.   doSetOKCancel(onOK, onCancel);
  189.  
  190.   param = window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  191.   if( !param ) {
  192.     return;
  193.   }
  194.  
  195.   param.SetInt(0, 1 );
  196.  
  197.   loadDialog();
  198. }
  199.  
  200. function onUnload()
  201. {
  202. }
  203.  
  204. function onOK()
  205. {
  206.   var print_paper_size = 0;
  207.  
  208.   if (prefs) {
  209.  
  210.     if (dialog.fileRadio.checked == true) {
  211.       prefs.SetBoolPref("print.print_tofile", true);
  212.     } else {
  213.       prefs.SetBoolPref("print.print_tofile", false);
  214.     }
  215.  
  216.     if (dialog.lastRadio.checked == true) {
  217.       prefs.SetBoolPref("print.print_reversed", true);
  218.     } else {
  219.       prefs.SetBoolPref("print.print_reversed", false);
  220.     }
  221.  
  222.     if (dialog.colorRadio.checked == true) {
  223.       prefs.SetBoolPref("print.print_color", true);
  224.     } else {
  225.       prefs.SetBoolPref("print.print_color", false);
  226.     }
  227.  
  228.     if (dialog.letterRadio.checked == true) {
  229.       print_paper_size = 0;
  230.     } else if (dialog.legalRadio.checked == true) {
  231.       print_paper_size = 1;
  232.     } else if (dialog.exectiveRadio.checked == true) {
  233.       print_paper_size = 2;
  234.     } else if (dialog.a4Radio.checked == true) {
  235.       print_paper_size = 3;
  236.     }
  237.     prefs.SetIntPref("print.print_paper_size", print_paper_size);
  238.  
  239.     prefs.SetIntPref("print.print_margin_top", dialog.topInput.value * 1000);
  240.     prefs.SetIntPref("print.print_margin_left", dialog.leftInput.value * 1000);
  241.     prefs.SetIntPref("print.print_margin_bottom", dialog.bottomInput.value *1000);
  242.     prefs.SetIntPref("print.print_margin_right", dialog.rightInput.value * 1000);
  243.  
  244.     prefs.SetCharPref("print.print_command", dialog.cmdInput.value);
  245.     prefs.SetCharPref("print.print_file", dialog.fileInput.value);
  246.   }
  247.  
  248.   if (param) {
  249.     param.SetInt(0, 0 );
  250.   }
  251.  
  252.   return true;
  253. }
  254.  
  255. function onCancel()
  256. {
  257.   if (param) {
  258.     param.SetInt(0, 1 );
  259.   }
  260.   return true;
  261. }
  262.  
  263. const nsIFilePicker = Components.interfaces.nsIFilePicker;
  264. function onChooseFile()
  265. {
  266.   if (dialog.fileRadio.checked == false) {
  267.     return;
  268.   }
  269.   try {
  270.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  271.     fp.init(window, document.getElementById("fpDialog").getAttribute("value"), nsIFilePicker.modeSave);
  272.     fp.appendFilters(nsIFilePicker.filterAll);
  273.     fp.show();
  274.     if (fp.file && fp.file.path.length > 0) {
  275.       dialog.fileInput.value = fp.file.path;
  276.     }
  277.   } catch(ex) {
  278.     dump(ex);
  279.   }
  280. }
  281.